{merge, once} = require 'lodash'
module.exports = (beforeRouteEnter, store)->
    state = undefined
    func = ->
        if state == undefined
            state = await beforeRouteEnter.apply(@,arguments)
        Object.assign(
            @store.state
            state
        )

    (mod)->
        require('./beforeRouteEnter')(
            mod
            (to, from, next)->
                await func.apply mod, arguments
                next()
        )
        merge(
            mod
            {
                store
            }
        )

